﻿@using System.Web.UI.WebControls
@using PagedList.Mvc;
@using RechercheEmplois.Models;
@model RechercheEmplois.ViewModel.OffresRecherchesVM

<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
    ViewBag.Title = "Offre d'Emplois";
    if (ViewBag.CurrentSortOrder == null)
    {
        ViewBag.CurrentSortOrder = "asc";
    }
    if (ViewBag.CurrentSort == null)
    {
        ViewBag.CurrentSort = "motcles";
    }
    List<string> listeProvines = null;
    List<string> listeSecteurs = null;
    string[] listeTypes = null;
    string[] listeNiveauEtudes = null;
    int remuneration = 0;
    string criteres = "";
    if (Session["SelectedProvines"] != null)
    {
        var provines = (List<Provine>)Session["SelectedProvines"];
        listeProvines = new List<string>();
        provines.ForEach(p => listeProvines.Add(p.Libelle));
        var strProvines = string.Join(", ", listeProvines);
        criteres += "Provines: " + strProvines + " ";
    }
    if (Session["SelectedSecteurs"] != null)
    {
        var secteurs = (List<Domaine>)Session["SelectedSecteurs"];
        listeSecteurs = new List<string>();
        secteurs.ForEach(p => listeSecteurs.Add(p.Libelle));
        var strSecteurs = string.Join(", ", listeSecteurs);
        if (!string.IsNullOrEmpty(criteres))
        {
            criteres += "; ";
        }
        criteres += "Secteurs: " + strSecteurs + " ";
    }
    if (Session["SelectedTypes"] != null)
    {
        listeTypes = (string[])Session["SelectedTypes"];
        var strTypes = string.Join(", ", listeTypes);
        if (!string.IsNullOrEmpty(criteres))
        {
            criteres += "; ";
        }
        criteres += "Types: " + strTypes + " ";
    }
    if (Session["SelectedNiveauEtudes"] != null)
    {
        listeNiveauEtudes = (string[])Session["SelectedNiveauEtudes"];
        var strNiveaux = string.Join(", ", listeNiveauEtudes);
        if (!string.IsNullOrEmpty(criteres))
        {
            criteres += "; ";
        }
        criteres += "Niveau d'Etude: " + strNiveaux + " ";
    }
    if (ViewBag.Remuneration != null)
    {
        remuneration = (int)ViewBag.Remuneration;
        if (!string.IsNullOrEmpty(criteres))
        {
            criteres += "; ";
        }
        criteres += "Remuneration: > " + remuneration + " ";
    }
    if (!string.IsNullOrEmpty(criteres))
    {
        criteres = "Les criteres selectées: " + criteres;
    }
}
<div class="rechercher-motcle">
    <div class="col-md-8 col-md-offset-2">
        @using (Html.BeginForm("Index", "Offre", new { DomaineSelectedID = ViewBag.DomaineSelectedID, ProvineSelectedID = ViewBag.ProvineSelectedID, OffreTypeSelectedID = ViewBag.OffreTypeSelectedID }))
        {
            <div class="input-group">
                @*<input id="searchKeys" type="text" class="form-control" placeholder="Saisir des mot clefs..." >*@
                @Html.TextBox("searchKeys", ViewBag.CurrentSearch as string, new { @class = "form-control", placeholder = "Saisir des mot clefs..." })
                <span class="input-group-btn">
                    <input type="submit" class="btn btn-info" style="width: 100%" value="Rechercher" />
                </span>
            </div>
        }            
    </div>
</div>
<div class="row">
    <div class="col-md-3">
        @using (Html.BeginForm("Index", "Offre", new { searchKeys = ViewBag.CurrentSearch }))
        {
            <div class="panel panel-default">
                <div class="panel-body">
                    <h4>Recherche Rapide</h4>
                    <hr/>
                    <div class="choisir-field">
                        @Html.DropDownList("DomaineSelectedID", Model.Domaines, "---Choisir un secteur---", new {@class = "selectpicker show-tick", data_live_search = "true"})
                    </div>
                    <div class="choisir-field">
                        @Html.DropDownList("ProvineSelectedID", Model.Provines, "---Choisir une provine---", new { @class = "selectpicker show-tick", data_live_search = "true" })
                    </div>
                    <div class="choisir-field">
                        @Html.DropDownList("OffreTypeSelectedID", Model.OffreTypes, "---Choisir un type de contract---", new { @class = "selectpicker show-tick" })
                    </div>
                    <hr/>
                    <input type="submit" class="btn btn-primary" style="width: 100%" value="RECHERCHER" />
                    <div class="rechercher-avancee">
                        @Html.ActionLink("RECHERCHER AVANCÉE", "RechercherAvancee", "Offre")
                    </div>
                </div>
            </div>
        }
    </div>
    <div class="col-md-6 panel panel-default">
        <h4>Offre d'emplois rétrouvés <span class="badge">@Model.Offres.Count()</span></h4>
        @if (!string.IsNullOrEmpty(criteres))
        {
            <div class="success alert-info">
                @criteres
            </div>
        }
        <div class="tri-dialog">
            <p class="label label-danger">Choisissez un attribut à trier: </p>
            <p>
                <select class="selectpicker show-tick triMenu" data-style="btn-info" data-width="auto">
                    <option value="motcles">Nombre de motcles correspondant</option>
                    <option value="titre">Titre du job</option>
                    <option value="domaine">Domaine de l'offre</option>
                    <option value="type">Type du job</option>
                    <option value="ville">Ville</option>
                    <option value="provine">Provine</option>
                </select>
                <select class="selectpicker show-tick sortOrder" data-style="btn-warning" data-width="15%">
                    <option value="asc" data-icon="fa fa-sort-alpha-asc">ASC</option>
                    <option value="desc" data-icon="fa fa-sort-alpha-desc">DES</option>
                </select>
            </p>

        </div>
        @foreach (var item in Model.OffresPaged) {
            <div class="panel panel-success">
                <div class="panel-heading">@Html.ActionLink(@item.Libelle, "Details", new { id = item.ID }, new { @class = "h5"})</div>
                <div class="panel-body">
                    <dl class="dl-horizontal">
                        <dt>Domaine</dt>
                        <dd>@Html.DisplayFor(modelItem => item.Domaine.Libelle)</dd>
                        <dt>Localtion</dt>
                        <dd>@Html.DisplayFor(modelItem => item.Ville.Libelle)</dd>
                        <dt>Job Type</dt>
                        <dd>@Html.DisplayFor(modelItem => item.Type)</dd>
                        <dt>Descriptions</dt>
                        <dd>@Html.DisplayFor(modelItem => item.Description)</dd>
                        <dt>Tags</dt>
                        <dd>
                            @foreach (var motcle in item.MotCles)
                            {
                                <span class="label label-primary">@motcle.Libelle</span>
                            }
                        </dd>
                    </dl>
                </div>
                <div class="panel-footer">
                    @Html.ActionLink("View Details", "Details", new { id = item.ID }, new { @class = "btn-sm btn-primary" })
                    @*@Html.ActionLink("Sauvergarder", "Details", new { id = item.ID }, new { @class = "btn-sm btn-warning" })*@
                    <a class="btn-sm btn-warning" onclick="saveOffre(@item.ID);">Sauvegarder</a>
                    @Html.ActionLink("Postuler", "Create", "Postuler", new { offreID = item.ID }, new { @class = "btn-sm btn-success" })
                </div>
            </div>
        }
        <br />
        Page @(Model.OffresPaged.PageCount < Model.OffresPaged.PageNumber ? 0 : Model.OffresPaged.PageNumber) of @Model.OffresPaged.PageCount
        @Html.PagedListPager(Model.OffresPaged, page => Url.Action("Index", new { page, triValeur = ViewBag.CurrentSort, triOrdre = ViewBag.CurrentSortOrder, currentFilter = ViewBag.CurrentFilter, DomaineSelectedID = ViewBag.DomaineSelectedID, ProvineSelectedID = ViewBag.ProvineSelectedID, OffreTypeSelectedID = ViewBag.OffreTypeSelectedID, remuValue = remuneration }))
    </div>
    <div class="col-md-2 panel panel-default">
        <h4 class="text-center">Sauvegardes <span class="badge">@Model.OffreSauvegardes.Count()</span></h4>
        @foreach (var item in Model.OffreSauvegardes)
        {
            <div class="panel panel-default">
                <div class="panel-heading">@Html.ActionLink(@item.Offre.Libelle, "Details", new { id = item.OffreID }, new { @class = "h5" })</div>
                <div class="panel-body">
                    @item.Offre.Description
                    <blockquote>
                        <small>@item.TempSauvegarde</small>
                    </blockquote>
                </div>
                <div class="panel-footer">
                    @*@Html.ActionLink("Supprimer", "Details", new { id = item.OffreID }, new { @class = "btn-xs btn-warning" })*@
                    <a class="btn-xs btn-warning" onclick="supprimerOffre(@item.SauvegardeID);">Supprimer</a>
                    @Html.ActionLink("Postuler", "Details", new { id = item.SauvegardeID }, new { @class = "btn-xs btn-success" })
                </div>
            </div>
        }
    </div>
    
    <div id='dialog-modal' style="display: none">
    </div>
    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        //inicialization of select picker
        $('.selectpicker').selectpicker();
        //load current sort
        $('.triMenu').selectpicker('val', '@ViewBag.CurrentSort');
        $('.sortOrder').selectpicker('val', '@ViewBag.CurrentSortOrder');
        //on change function i need to control selected value
        $('select.triMenu').on('change', function () {
            reloadPage();
            @*var selectedSortOrder = $('.sortOrder option:selected').val();
            var selectedSort = $('.triMenu option:selected').val();
            var url = '@Url.Action("Index", "Offre")';
            $.redirectPost(url, { triOrdre: selectedSortOrder, triValeur: selectedSort, DomaineSelectedID: '@ViewBag.DomaineSelectedID', ProvineSelectedID: '@ViewBag.ProvineSelectedID', OffreTypeSelectedID: '@ViewBag.OffreTypeSelectedID', searchKeys: '@ViewBag.CurrentSearch' });*@
        });
        $('select.sortOrder').on('change', function () {
            reloadPage();
            @*var selectedSortOrder = $('.sortOrder option:selected').val();
            var url = '@Url.Action("Index", "Offre")';
            var selectedSort = $('.triMenu option:selected').val();
            $.redirectPost(url, { triValeur: selectedSort, triOrdre: selectedSortOrder, DomaineSelectedID: '@ViewBag.DomaineSelectedID', ProvineSelectedID: '@ViewBag.ProvineSelectedID', OffreTypeSelectedID: '@ViewBag.OffreTypeSelectedID', searchKeys: '@ViewBag.CurrentSearch' });*@
        });
        // jquery extend function
        $.extend(
        {
            redirectPost: function (location, args) {
                var form = '';
                $.each(args, function (key, value) {
                    value = value.split('"').join('\"');
                    form += '<input type="hidden" name="' + key + '" value="' + value + '">';
                });
                //alert(form);
                $('<form action="' + location + '" method="POST">' + form + '</form>').appendTo($(document.body)).submit();
            }
        });
        //$(document).ready(saveOffre);

    });
    //Fonction recharger page
    function reloadPage() {
        var selectedSortOrder = $('.sortOrder option:selected').val();
        var url = '@Url.Action("Index", "Offre")';
        var selectedSort = $('.triMenu option:selected').val();
        $.redirectPost(url, { triValeur: selectedSort, triOrdre: selectedSortOrder, DomaineSelectedID: '@ViewBag.DomaineSelectedID', ProvineSelectedID: '@ViewBag.ProvineSelectedID', OffreTypeSelectedID: '@ViewBag.OffreTypeSelectedID', searchKeys: '@ViewBag.CurrentSearch' });
    }

    //Sauvegarder buttons click
    function saveOffre(id) {
        // e.preventDefault(); use this or return false
        $.ajax({
            url: "/Sauvegarde/Create",
            type: "post",
            data: "OffreID=" + id,
            success: function (result) {
                var response;
                if (result == 1) {
                    response = "Offre est sauvé succès!";
                }
                else if (result == 0) {
                    response = "Offre a été déjà sauvé!";
                } else {
                    response = "Utilisateur non identifié!";
                }
                var url = '/Offre/GetMessage?title=SaveInformation&body=' + encodeURI(response);
                $("#myModal").on("show.bs.modal", function (e) {
                    $(e.target).find('.modal-content').load(url);
                });
                $('#myModal').modal('show');
                $('#myModal').on('hidden.bs.modal', function () {
                    if (result == 1)
                        reloadPage();
                });
            },
            error: function () {
                alert("technique failure!");
            }
        });
    }
    //Supprimer offre sauvegarde click
    function supprimerOffre(id) {
        // e.preventDefault(); use this or return false
        $.ajax({
            url: "/Sauvegarde/Delete",
            type: "post",
            data: "id=" + id,
            success: function (result) {
                var response = "";
                if (result == -1) {
                    response = "Ne trouver pas l'offre qui a besoin de supprimer!";
                }
                else if (result == 0) {
                    response = "L'offre sauvegardé a été supprimé!";
                }
                var url = '/Offre/GetMessage?title=SaveInformation&body=' + encodeURI(response);
                $("#myModal").on("show.bs.modal", function (e) {
                    $(e.target).find('.modal-content').load(url);
                });
                $('#myModal').modal('show');
                $('#myModal').on('hidden.bs.modal', function () {
                    if (result == 0)
                        reloadPage();
                });
            },
            error: function () {
                alert("technique failure!");
            }
        });
    }
</script>
